from django.shortcuts import render
from .ser import *
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.paginator import Paginator


# Create your views here.

class User_API(APIView):
    def post(self,request):
        ser = User_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code": 10010})

    def get(self,request):
        u = Department.objects.all()
        ser = Department_Ser(u,many=True)
        return Response({"code":200,"list":ser.data})

class User2_API(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        u = User.objects.filter(username=username).first()
        if u:
            if u.password == password:
                return Response({"code":200,"userid":u.id,'authrity':u.authrity})
            else:
                return Response({"code":"密码不对"})
        else:
            return Response({"code": "用户名不存在"})



class Department_API(APIView):
    def post(self,request):
        ser = Department_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code": 10010})

    def get(self,request):
        u = User.objects.all()
        cpage = request.GET.get('cpage')
        pag = Paginator(u,3)
        tpage = pag.page(cpage)
        num = pag.num_pages
        ser = showUser_Ser(tpage,many=True)
        return Response({"code":200,"list":ser.data,"num":num})


class User_show(APIView):
    def get(self,request):
        name = request.GET.get("name")
        u = User.objects.filter(username=name).first()
        ser = showUser_Ser(u)
        if u:
            return Response({"code":200,"list":ser.data})
        else:
            return Response({"code":"没有这个人"})

class Qusers_API(APIView):
    def post(self,request):
        username = request.data.get("username")
        password = request.data.get("password")
        q = Qusers.objects.filter(username=username).first()
        if q:
            if q.password == password:
                return Response({"code":200,'userid':q.id})
        else:
            return Response({"code":"登录失败"})


class Goods_API(APIView):
    def get(self,request):
        g = Goods.objects.all()
        cpage = request.GET.get("cpage")
        pag = Paginator(g,2)
        num = pag.num_pages
        t = pag.page(cpage)
        ser = Goods_Ser(t,many=True)
        return Response({"code":200,"list":ser.data,'num':num})

class Car_API(APIView):
    def post(self,request):
        print(request.data)
        qusers  = request.data.get("userid")
        goods  = request.data.get("goods")
        num = request.data.get("num")
        c = Car.objects.filter(goods_id=goods,qusers_id=qusers).first()
        if c:
            c.num += num
            c.save()
        else:
            Car.objects.create(goods_id=goods,qusers_id=qusers,num=num)
        return Response({"code":200})

from utils.common import *
import random
class AAA(APIView):
    def get(self,request):
       print(send_message('15309518018',"haha",1))
       return Response({"code":200})


class SedSms(APIView):
    def post(self,request):
        sjh = request.data.get('sjh')
        a = random.randint(100000,999999)
        res = send_message(sjh,a,1)
        if res:
            conn = get_redis()
            conn.setex(sjh,500,a)
            return Response({"code":200})
        return Response({"code":20010})

    def get(self,request):
        yzm = request.GET.get("yzm")
        sjh = request.GET.get('sjh')
        conn = get_redis()
        c = conn.get(sjh)
        if c:
            c = str(c,encoding="utf8")
        if yzm == c:
            return Response({"code":200})

class X(APIView):
    def post(self,request):
        ser = Zhuce_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})


class Nusers_API(APIView):
    def post(self,request):
        username = request.data.get("username")
        password = request.data.get("password")
        q = Nuser.objects.filter(username=username).first()
        if q:
            if q.password == password:
                return Response({"code":200,'userid':q.id})
        else:
            return Response({"code":"登录失败"})

class News_API(APIView):
    def get(self,request):
        id = request.GET.get("id")
        userid = request.GET.get("userid")
        print(id)
        if not id:
            n = News.objects.all()
            cpage = request.GET.get("cpage")
            pag = Paginator(n,2)
            t = pag.page(cpage)
            num = pag.num_pages
            ser = News_Ser(t,many=True)
            return Response({"code":200,"list":ser.data,'num':num})
        else:
            n = News.objects.filter(id = id).first()
            ser = News_Ser(n)
            conn = get_redis()
            n = conn.hincrby(id,userid)
            if n:
                n = int(n)
            if n>3:
                return Response({"code":10013})
            else:
                return Response({"code": 200, "list": ser.data})


class Hhh(APIView):
    def post(self,request):
        sjh = request.data.get("sjh")
        reg = random.randint(1000,9999)
        res = send_message(sjh,reg,1)
        if res:
            conn = get_redis()
            conn.setex(sjh,1000,reg)
            return Response({"code":200})
        return Response({"code":10010})

class Add_(APIView):
    def post(self,request):
        yzm = request.data.get("yazm")
        sjh = request.data.get("sjh")
        # conn = get_redis()
        # c = conn.get(sjh)
        # if c:
        #     c = str(c,encoding="utf8")
        # if yzm == c:
        #     # ser = Zhuce_Ser(data=)
        #     return Response({"code":200})
        return Response({"code":10010})


class Diany_API(APIView):
    def get(self,request):
        id = request.GET.get("id")
        if not id:
            d = Diany.objects.all()
            cpage = request.GET.get("cpage")
            pag = Paginator(d,2)
            t = pag.page(cpage)
            num = pag.num_pages
            ser = Diany_Ser(t,many=True)
            return Response({"code":200,'list':ser.data,"num":num})
        else:
            dd = Diany.objects.filter(id=id).first()
            ser = Diany_Ser(dd)
            return Response({"code":200,"list":ser.data})

class Actor_API(APIView):
    def get(self,request):
        name = request.GET.get("actor")
        a = Actor.objects.filter(name=name).first()
        d = Diany.objects.filter(actor_id=a.id).all()
        ser = Diany_Ser(d,many=True)
        return Response({"code":200,"list":ser.data})

class Teacher_API(APIView):
    def post(self,request):
        username = request.data.get("username")
        password = request.data.get("password")
        t = Teacher.objects.filter(username=username).first()
        if t:
            if t.password == password:
                return Response({"code":200,'userid':t.id})
        return Response({"code":10010})


class Student_API(APIView):
    def post(self,request):
        ser = Student_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})

    def get(self,request):
        userid = request.GET.get("userid")
        s = Student.objects.filter(teacher_id=userid).all()
        ser = Student_Ser(s,many=True)
        return Response({"code":200,'list':ser.data})

class Course_API(APIView):
    def get(self,request):
        s = Sclass.objects.all()
        ser = Sclass_Ser(s,many=True)
        return Response({"code":200,"list":ser.data})

    def post(self,request):
        username = request.data.get("username")
        password = request.data.get("password")
        s = Stu.objects.filter(username=username).first()
        if s:
            if s.password == password:
                return Response({"code":200,'userid':s.id,'name':s.name})
            else:
                return Response({"code":10010})
        else:
            return Response({"code":10011})

    def put(self,request):
        userid = request.GET.get('userid')
        s = Stu.objects.filter(id = userid).first()
        stus = Sshow_Ser(s).data
        if stus['cla']['zt']=='1':
            cll = Sclass.objects.filter(zt=2).first()
            s.cla_id = cll.id
            s.save()
            return Response({"code": 200})
        elif stus['cla']['zt']=='2':
            cll = Sclass.objects.filter(zt=3).first()
            s.cla_id = cll.id
            s.save()
            return Response({"code": 200})
        else:
            return Response({"code":10010})


class Sclass_API(APIView):
    def get(self,request):
        s = Sclass.objects.all()
        ser = showSclass_Ser(s,many=True)
        return Response({"code":200,"list":ser.data})

    def post(self,request):
        ser = Stu_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            return Response({"code":10010})

class SUser_API(APIView):
    def get(self,request):
        userid = request.GET.get('userid')
        s = Stu.objects.filter(id=userid).first()
        ser = showstu1_Ser(s)
        return Response({"code":200,"list":ser.data})

    def post(self,request):
        goods = request.data.get('goods')
        ser = Spl_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            s = Sgoods.objects.filter(id=goods).first()
            s.pl_count+=1
            s.save()
            return Response({"code":200})
        else:
            return Response({"code":1009})

class Spl_API(APIView):
    def get(self,request):
        goods = request.GET.get('goods')
        user = request.GET.get('user')
        s = Spl.objects.filter(goods_id=goods,user_id=user).all()
        ser = showSpl_Ser(s,many=True)
        return Response({"code":200,"list":ser.data})


class ShangDian_API(APIView):
    def post(self,request):
        name = request.data.get("name")
        password = request.data.get("password")
        s = ShangDian.objects.filter(name=name).first()
        if s:
            if s.password == password:
                return Response({"code":200,"userid":s.id,'name':name})
            else:
                return Response({"code":10010})
        else:
            return Response({"code": 10011})

    def get(self,request):
        cpage = request.GET.get("cpage")
        userid = request.GET.get("user")
        s = Sgoods.objects.filter(shangdian_id=userid).all().order_by('-'+'price')
        pag = Paginator(s,2)
        t = pag.page(cpage)
        ser = Sgoods_Ser(t,many=True)
        num = pag.num_pages
        return Response({"code":200,'list':ser.data,'num':num})

    def delete(self,request):
        goods_id = request.GET.get("id")
        Sgoods.objects.filter(id=goods_id).delete()
        return Response({"code":200})




class Ttecher_API(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        t = Ttecher.objects.filter(name=name).first()
        if t:
            if t.password == password:
                return Response({"code":200,"userid":t.id,'name':name})
            else:
                return Response({"code": 11000})
        return Response({"code":10000})

class Tstu_API(APIView):
    def post(self,request):
        ser = Tstu_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            return Response({"code":10010})

    def get(self,request):
        userid = request.GET.get('userid')
        t = Tstu.objects.filter(techer_id=userid).all()
        ser = showTstu_Ser(t,many=True)
        return Response({"code":200,"list":ser.data})

    def delete(self,request):
        id = request.GET.get('id')
        t = Tstu.objects.filter(id=id).first()
        t.techer = None
        t.save()
        return Response({"code":200})

class Tstu_null_API(APIView):
    def get(self,request):
        t = Tstu.objects.filter(techer=None).all()
        ser = Tstu_Ser(t,many=True)
        return Response({"code":200,"list":ser.data})

    def put(self,request):
        id = request.GET.get('id')
        userid = request.GET.get('userid')
        t = Tstu.objects.filter(id=id).first()
        t.techer_id = userid
        t.save()
        return Response({"code":200})

class Xuser_API(APIView):
    def post(self,request):
        name = request.data.get("username")
        password = request.data.get("password")
        s = Xuser.objects.filter(username=name).first()
        if s:
            if s.password == password:
                return Response({"code":200,"userid":s.id,'name':name})
            else:
                return Response({"code":10010})
        else:
            return Response({"code": 10011})

    def get(self,request):
        x = Xiaoqu.objects.all()
        ser = Xiaoqu_Ser(x,many=True)
        return Response({"code":200,"list":ser.data})

class Xclass_API(APIView):
    def get(self,request):
        x = Xclass.objects.all()
        ser = showXclass_Ser(x, many=True)
        return Response({"code": 200, "list": ser.data})

class XXstu_API(APIView):
    def get(self,request):
        userid = request.GET.get('userid')
        s = Xstu.objects.filter(xuser_id=userid).all()
        ser = showXstu_Ser(s,many=True)
        return Response({"code":200,"list":ser.data})































